;;; package --- Summary hello
;;; Commentary:
;;; Code:

;; get request
(defun get (url buffername)
  "URL .  BUFFERNAME: ."
  (progn  (with-current-buffer (get-buffer-create "*wework*")
            (insert (format "\n---------------%s--------------\nrequest:\n" buffername))
            (insert (format "%s\n\n" url)))
          (request
            url
            :type "GET"
            :parser 'buffer-string
            :sync t
            :success  (cl-function
                       (lambda (&key response &allow-other-keys)
                         (progn (message "finished!\n" )
                                (with-current-buffer (get-buffer-create "*wework*")
                                  (insert (format "response:\n\n%s\n" (request-response--raw-header response)))
                                  (insert (format "%s\n" (request-response-data response)))
                                  (pop-to-buffer (current-buffer)))
                                )))
            :error (cl-function (lambda (&rest args &key error-thrown &allow-other-keys)
                                  (with-current-buffer (get-buffer-create "*request demo*")
                                    (insert (format "response ERROR:\n\n%S\n" error-thrown))
                                    (pop-to-buffer (current-buffer)))))  )

          ))


(defun post_json (url data buffername)
  "URL . DATA . BUFFERNAME ."
  (progn (with-current-buffer (get-buffer-create "*wework*")
           (insert (format "\n---------------%s--------------\nrequest:\n" buffername))
           (insert (format "%s\n\n" url)))
         (request
           url
           :type "POST"
           :data (json-encode data)
           :headers '(("Content-Type" . "application/json"))
           :parser 'buffer-string
           :sync t
           :success (cl-function
                     (lambda (&key response &allow-other-keys)
                       (progn (message "finished: %s\n" (request-response-data response))
                              (with-current-buffer (get-buffer-create "*request demo*")
                                (insert (format "---------------------------------\n%s\n" buffername))
                                (insert (format "response header:\n%s\n" (request-response-header response)))
                                (insert (format "response:\n%s\n" (request-response-data response)))
                                (pop-to-buffer (current-buffer))))))
           :error (cl-function (lambda (&rest args &key error-thrown &allow-other-keys)
                                 (with-current-buffer (get-buffer-create "*request demo*")
                                   (insert (format "response ERROR:\n\n%S\n" error-thrown))
                                   (pop-to-buffer (current-buffer)))))
           ))
  )

(defun access_token (corpid appsecret)
  "CORPID . APPSECRET ."
  
  (let ((url (format "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=%s&corpsecret=%s" corpid appsecret)))
    (get url "access_token")))

(provide 'wework-api)
;;; wework-api.el ends here
